﻿<UserControl
    x:Class="UWPCore.Framework.Controls.OneDriveControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPCore.Framework.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converter="using:UWPCore.Framework.Converters"
    xmlns:designTime="using:UWPCore.Framework.ViewModels.DesignTime"
    mc:Ignorable="d"
    d:DesignHeight="800"
    d:DesignWidth="480">

    <UserControl.DataContext>
        <designTime:OneDriveViewModel />
    </UserControl.DataContext>

    <UserControl.Resources>
        <converter:BooleanNegationConverter x:Key="BoolNegationConverter"/>
        <converter:DateTimeConverter x:Key="DateTimeConverter"/>
    </UserControl.Resources>
    
    <Grid>
        <!-- Upload and Download Pane -->
        <Grid Grid.Row="1"               
              EntranceNavigationTransitionInfo.IsTargetElement="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!-- Status Content -->
            <Grid Grid.Row="0">
                <StackPanel>
                    <ProgressBar IsIndeterminate="{Binding BackupIsIndeterminate}" />
                    <TextBlock Text="{Binding BackupStatusText}" Margin="8,0,8,0"/>
                </StackPanel>
            </Grid>

            <!-- Upload Content -->
            <Grid Grid.Row="1" Margin="8,12,8,0">
                <StackPanel>
                    <TextBlock Text="Upload Backup:" Style="{StaticResource SubtitleTextBlockStyle}" Margin="0,0,0,8"/>
                    <TextBlock TextWrapping="WrapWholeWords" Margin="0,0,0,4">
                        <Run Text="File saved in OneDrive:"/>
                        <Run Text="{Binding UploadPath}"/>
                    </TextBlock>
                    <Grid Margin="0,0,0,4">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="Name:"
                                   VerticalAlignment="Center" Margin="0,0,8,0"/>
                        <TextBox Grid.Column="1"
                                 Text="{Binding UploadName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                 IsEnabled="{Binding BackupIsIndeterminate, Converter={StaticResource BoolNegationConverter}}"/>
                    </Grid>
                    <TextBlock Text="{Binding UploadValidationText}" TextWrapping="WrapWholeWords"
                               Foreground="{StaticResource SystemControlHighlightAccentBrush}"/>

                    <Button Content="Upload" Command="{Binding UploadCommand}" Margin="0,12,0,0"
                            Width="100"
                            IsEnabled="{Binding BackupIsIndeterminate, Converter={StaticResource BoolNegationConverter}}"/>
                </StackPanel>
            </Grid>

            <!-- Download Content -->
            <Grid Grid.Row="2" Margin="8,12,8,0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <TextBlock Text="Download Backup:" Style="{StaticResource SubtitleTextBlockStyle}" Margin="0,0,0,8"/>
                </Grid>

                <Grid Grid.Row="1">
                    <ListView ItemsSource="{Binding OneDriveItems}" IsEnabled="{Binding BackupIsIndeterminate, Converter={StaticResource BoolNegationConverter}}" SelectedItem="{Binding SelectedOneDriveItem, Mode=TwoWay}"
                          SelectionMode="Single">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>

                                    <TextBlock Grid.Row="0" 
                                               Text="{Binding Name}" Style="{StaticResource BodyTextBlockStyle}" FontSize="18" />

                                    <Grid Grid.Row="1" Margin="0,0,4,4">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="200" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>

                                        <TextBlock Grid.Column="0" Text="{Binding LastModifiedDateTime.DateTime, Converter={StaticResource DateTimeConverter}}" Foreground="Gray"/>
                                        <TextBlock Grid.Column="1" Foreground="Gray">
                                            <Run Text="{Binding Size}"/>
                                             <Run Text="bytes"/>
                                        </TextBlock>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>

                <Grid Grid.Row="2" Margin="0,12,0,8">
                    <Button Content="Download" Command="{Binding DownloadCommand}" 
                            Width="100"
                            IsEnabled="{Binding BackupIsIndeterminate, Converter={StaticResource BoolNegationConverter}}"/>
                </Grid>
            </Grid>
        </Grid>

        <!-- SignUp Pane-->
        <Grid Grid.Row="1"              
              EntranceNavigationTransitionInfo.IsTargetElement="True"
              Visibility="{Binding SignInPaneVisibility}"
              Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ProgressBar Grid.Row="0" 
                         IsIndeterminate="{Binding SignInIsIndeterminate}" />
            <Grid Grid.Row="1" 
                  VerticalAlignment="Center">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0"
                           HorizontalAlignment="Center"
                           Text="{Binding SignInStatusText}" TextWrapping="Wrap"/>
                <Button Grid.Row="1" 
                        Content="Retry" 
                        HorizontalAlignment="Center" Width="100" Margin="0,10"
                        Command="{Binding SignInRetryCommand}"/>
            </Grid>
        </Grid>
    </Grid>
</UserControl>
